# -*- encoding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2011 ZestyBeanz Technologies Pvt. Ltd.
#    (http://wwww.zbeanztech.com) All Rights Reserved.
#     vishnu@zbeanztech.com
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import osv, fields
import base64
import csv
from tempfile import TemporaryFile
import tools
from tools.translate import _

class email_import(osv.osv_memory):
    _name = 'email.import'
    _columns = {
                'file': fields.binary('File', required=True),

                }
    
    def import_email(self, cr, uid, ids, context=None):
        
        pol_browse = self.browse(cr,uid,ids[0])
        if not pol_browse.file:
            return True
        
        fileobj = TemporaryFile('w+')
        try:
            fileobj.write(base64.decodestring(pol_browse.file))
        
            # now we determine the file format
            fileobj.seek(0)
            first_line = fileobj.readline().strip().replace('"', '').replace(' ', '')
            fileformat = 'csv'
            fileobj.seek(0)
            
            spamreader = csv.reader(fileobj, delimiter=',', quotechar='|')
            
            for row in spamreader:
                mem_data = {
                            'email': row[0],
                            'prospect' : row[0],
                            'mail_group_id':context['active_id'],
                            'not_interested': True
                            }

                self.pool.get('mail.group.member').create(cr, uid, mem_data, context=context)
        finally:
            fileobj.close()
        return {'type': 'ir.actions.act_window_close'}

#    def import_email(self,cr, uid, ids, context=None):
#
#        (file1,) = self.browse(cr, uid, ids , context=context)
#        file_data = file1.file
#        a= base64.decodestring(file_data)
#        path = os.getcwd()
#        #***************
#        file=open(path + '/addons/zb_mass_mail/wizard/file.xls','w')
#        file.write(base64.b64decode(file_data))
#        file.close()
#
#        wb = xlrd.open_workbook(path + '/addons/zb_mass_mail/wizard/file.xls')
#
#        sh = wb.sheet_by_index(0)
#        sh = wb.sheet_by_name(u'Sheet1')
#        a=[]
#        for rownum in range(sh.nrows):
#            a.append(sh.row_values(rownum)[0])
#        #x = list(set(a)),
#        k=dict.fromkeys(a).keys()
#        #k=k.keys()
#        act_id = context['active_id']
#        not_interested = False
#
#        for i in range(len(k)):
#            ser_data = self.pool.get('mail.group.member').search(cr, uid, [("email", '=', k[i]),('mail_group_id','=',act_id)])
#            if self.pool.get('mail.group.member').search(cr,uid,[("email", '=', k[i]),('not_interested','=',True)]) :
#                not_interested = True
#
#            if not ser_data:
#                mem_data = {
#                            'email':k[i],
#                            'mail_group_id':act_id,
#                            'not_interested': not_interested
#                            }
#
#                self.pool.get('mail.group.member').create(cr, uid, mem_data, context=context)

#        return {'type': 'ir.actions.act_window_close'}

email_import()